package com.anmoyi.web.controller;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.anmoyi.common.AppError;
import com.anmoyi.common.Packet;
import com.anmoyi.common.StringUtil;
import com.anmoyi.common.TokenUtil;
import com.anmoyi.common.exception.TokenException;
import com.anmoyi.model.po.Custommassage;
import com.anmoyi.model.po.Custommassagecontent;
import com.anmoyi.model.po.User;
import com.anmoyi.service.CustommassageService;
import com.anmoyi.service.CustommassagecontentService;
import com.anmoyi.service.UserService;
import com.anmoyi.web.ao.CustommassageAO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.UUID;

@RestController
public class CustommassageController extends BaseController{
    private static final Logger logger = LoggerFactory.getLogger(CustommassageController.class);
    @Autowired
    private CustommassagecontentService custommassagecontentService;


    @Autowired
    private CustommassageService custommassageService;


    @Autowired
    private UserService userService;


    @PostMapping(value = "/addCustommassage")
    public String addCustommassage(@RequestBody String requestString){
        logger.info("自定义按摩手法列数是: ");
        logger.info(requestString);


        Packet packet = null;

        try {
            packet = JSON.parseObject(requestString, Packet.class);
        } catch (Exception e) {
            logger.error("自定义按摩手法参数异常1\n" + e);
            return responseToClient(AppError.APP_JSON_INVALID_ERROR);
        }


        CustommassageAO custommassageAO = null;
        try {
            //packet.getData() 为jsonobject
            custommassageAO = JSON.parseObject(packet.getData().toString(), CustommassageAO.class);
        } catch (Exception e) {
            logger.error("自定义按摩手法参数异常2",  e);
            return responseToClient(AppError.APP_ARGS_ERROR);
        }



        if (StringUtil.isNullOrBlank(custommassageAO.getCname())){
            logger.error("自定义按摩手法参数异常3");
            return responseToClient(AppError.APP_ARGS_ERROR);
        }




        String phone = null;
        try {
            phone = TokenUtil.getEmail(packet.getToken());
        } catch (TokenException e) {
            logger.error("自定义按摩手法参数异常4",e);
            return responseToClient(AppError.APP_TOKEN_INVALID_ERROR);
        }

        User user = userService.getByPhone(phone);
        if (null == user){
            logger.error("自定义按摩手法参数异常5");
            return responseToClient(AppError.APP_TOKEN_INVALID_ERROR);
        }

        this.token = user.getToken();


        Custommassage custommassage = new Custommassage();
        custommassage.setUserId(user.getId());
        custommassage.setCname(custommassageAO.getCname());


        custommassageService.addCustommassage(custommassage);

        logger.info("自定义按摩手法参数异常6");

        return responseToClient(AppError.APP_OK);

    }
    @PostMapping(value = "/updateCustommassage")
    public String updateCustommassage(@RequestBody String requestString){
        logger.info("更新自定义按摩手法列数是: ");
        logger.info(requestString);


        Packet packet = null;

        try {
            packet = JSON.parseObject(requestString, Packet.class);
        } catch (Exception e) {
            logger.error("更新自定义按摩手法参数异常1\n" + e);
            return responseToClient(AppError.APP_JSON_INVALID_ERROR);
        }


        CustommassageAO custommassageAO = null;
        try {
            //packet.getData() 为jsonobject
            custommassageAO = JSON.parseObject(packet.getData().toString(), CustommassageAO.class);
        } catch (Exception e) {
            logger.error("更新自定义按摩手法参数异常2",  e);
            return responseToClient(AppError.APP_ARGS_ERROR);
        }



        if (StringUtil.isNullOrBlank(custommassageAO.getCname())){
            logger.error("自定义按摩手法参数异常3");
            return responseToClient(AppError.APP_ARGS_ERROR);
        }




        String phone = null;
        try {
            phone = TokenUtil.getEmail(packet.getToken());
        } catch (TokenException e) {
            logger.error("更新自定义按摩手法参数异常4",e);
            return responseToClient(AppError.APP_TOKEN_INVALID_ERROR);
        }

        User user = userService.getByPhone(phone);
        if (null == user){
            logger.error("更新自定义按摩手法参数异常5");
            return responseToClient(AppError.APP_TOKEN_INVALID_ERROR);
        }

        this.token = user.getToken();


        Custommassage custommassage = new Custommassage();
        custommassage.setId(custommassageAO.getId());
        custommassage.setUserId(user.getId());
        custommassage.setCname(custommassageAO.getCname());

        custommassageService.updateByPrimaryKeySelective(custommassage);

        logger.info("更新自定义按摩手法参数异常6");

        return responseToClient(AppError.APP_OK);

    }
    @PostMapping(value = "/deleteCustommassage")
    public String deleteCustommassage(@RequestBody String requestString){
        logger.info("自定义按摩手法列数是: ");
        logger.info(requestString);

        CustommassageAO custommassageAO = new CustommassageAO();
        String phone = null;
        try {
            //packet.getData() 为jsonobject
            System.out.println(requestString);
            JSONObject data=JSONObject.parseObject(requestString);
            JSONObject jo=data.getJSONObject("data");
            custommassageAO.setId(jo.getString("id"));
            //custommassageAO = JSON.parseObject(packet.getData().toString(), CustommassageAO.class);
            try {
                phone = TokenUtil.getEmail(data.getString("token"));
            } catch (TokenException e) {
                logger.error("自定义按摩手法参数异常4",e);
                return responseToClient(AppError.APP_TOKEN_INVALID_ERROR);
            }

        } catch (Exception e) {
            logger.error("自定义按摩手法参数异常2",  e);
            return responseToClient(AppError.APP_ARGS_ERROR);
        }


        User user = userService.getByPhone(phone);
        if (null == user){
            logger.error("自定义按摩手法参数异常5");
            return responseToClient(AppError.APP_TOKEN_INVALID_ERROR);
        }

        this.token = user.getToken();
        custommassageService.deleteByPrimaryKey(custommassageAO.getId());
        logger.info("更新自定义按摩手法参数异常6");
        return responseToClient(AppError.APP_OK);
    }
    @PostMapping(value = "/queryCustommassage")
    public String queryCustommassage(@RequestBody String requestString){
        logger.info("自定义按摩手法列数是: ");
        logger.info(requestString);

        CustommassageAO custommassageAO = new CustommassageAO();
        String phone = null;
        try {
            //packet.getData() 为jsonobject
            System.out.println(requestString);
            JSONObject data=JSONObject.parseObject(requestString);
            JSONObject jo=data.getJSONObject("data");
            //custommassageAO = JSON.parseObject(packet.getData().toString(), CustommassageAO.class);
            try {
                phone = TokenUtil.getEmail(data.getString("token"));
            } catch (TokenException e) {
                logger.error("自定义按摩手法参数异常4",e);
                return responseToClient(AppError.APP_TOKEN_INVALID_ERROR);
            }

        } catch (Exception e) {
            logger.error("自定义按摩手法参数异常2",  e);
            return responseToClient(AppError.APP_ARGS_ERROR);
        }


        User user = userService.getByPhone(phone);
        if (null == user){
            logger.error("自定义按摩手法参数异常5");
            return responseToClient(AppError.APP_TOKEN_INVALID_ERROR);
        }

        this.token = user.getToken();
        List<Custommassage> list=custommassageService.selectByuserId(user.getId());
        logger.info("更新自定义按摩手法参数异常6");
        return responseToClientWithData(AppError.APP_OK,list);
    }

    @PostMapping(value = "/addALLCustommassage")
    public String addALLCustommassage(@RequestBody String requestString){
        logger.info("自定义按摩手法列数是: ");
        logger.info(requestString);

        CustommassageAO custommassageAO = new CustommassageAO();
        String phone = null;
        try {
            //packet.getData() 为jsonobject
            System.out.println(requestString);
            JSONObject data=JSONObject.parseObject(requestString);
            JSONObject jo=data.getJSONObject("data");
            custommassageAO.setCname(jo.getString("cname"));
            custommassageAO.setJsonArraystr(jo.getJSONArray("JsonArraystr").toString());
            //custommassageAO = JSON.parseObject(packet.getData().toString(), CustommassageAO.class);
            try {
                phone = TokenUtil.getEmail(data.getString("token"));
            } catch (TokenException e) {
                logger.error("自定义按摩手法参数异常4",e);
                return responseToClient(AppError.APP_TOKEN_INVALID_ERROR);
            }

        } catch (Exception e) {
            logger.error("自定义按摩手法参数异常2",  e);
            return responseToClient(AppError.APP_ARGS_ERROR);
        }

        if (StringUtil.isNullOrBlank(custommassageAO.getCname())){
            logger.error("自定义按摩手法参数异常3");
            return responseToClient(AppError.APP_ARGS_ERROR);
        }

        User user = userService.getByPhone(phone);
        if (null == user){
            logger.error("自定义按摩手法参数异常5");
            return responseToClient(AppError.APP_TOKEN_INVALID_ERROR);
        }

        this.token = user.getToken();

        Custommassage custommassage = new Custommassage();
        String uuid = UUID.randomUUID().toString().replaceAll("-", "");
        custommassage.setId(uuid);
        custommassage.setUserId(user.getId());
        custommassage.setCname(custommassageAO.getCname());
        custommassage.setJsonArraystr(custommassageAO.getJsonArraystr());
        custommassageService.addCustommassage(custommassage);
       /* JSONArray ja=JSON.parseArray(custommassageAO.getJsonArraystr());
        for(int i=0;i<ja.size();i++) {
            System.out.println(ja.getJSONObject(i).get("id"));
            Custommassagecontent custommassagecontent = new Custommassagecontent();
            custommassagecontent.setCid(uuid);
            custommassagecontent.setMassagetechnique(ja.getJSONObject(i).get("massagetechnique")+"");
            custommassagecontent.setMassagetime(ja.getJSONObject(i).get("massagetime")+"");
            custommassagecontent.setCmd(ja.getJSONObject(i).get("cmd")+"");

            custommassagecontentService.addCustommassagecontent(custommassagecontent);
        }*/

        logger.info("自定义按摩手法参数异常6");

        return responseToClient(AppError.APP_OK);

    }
    @PostMapping(value = "/updateALLCustommassage")
    public String updateALLCustommassage(@RequestBody String requestString){
        logger.info("自定义按摩手法列数是: ");
        logger.info(requestString);

        CustommassageAO custommassageAO = new CustommassageAO();
        String phone = null;
        try {
            //packet.getData() 为jsonobject
            System.out.println(requestString);
            JSONObject data=JSONObject.parseObject(requestString);
            JSONObject jo=data.getJSONObject("data");
            custommassageAO.setId(jo.getString("id"));
            custommassageAO.setCname(jo.getString("cname"));
            custommassageAO.setJsonArraystr(jo.getJSONArray("JsonArraystr").toString());
            //custommassageAO = JSON.parseObject(packet.getData().toString(), CustommassageAO.class);
            try {
                phone = TokenUtil.getEmail(data.getString("token"));
            } catch (TokenException e) {
                logger.error("自定义按摩手法参数异常4",e);
                return responseToClient(AppError.APP_TOKEN_INVALID_ERROR);
            }

        } catch (Exception e) {
            logger.error("自定义按摩手法参数异常2",  e);
            return responseToClient(AppError.APP_ARGS_ERROR);
        }

        if (StringUtil.isNullOrBlank(custommassageAO.getCname())){
            logger.error("自定义按摩手法参数异常3");
            return responseToClient(AppError.APP_ARGS_ERROR);
        }

        User user = userService.getByPhone(phone);
        if (null == user){
            logger.error("自定义按摩手法参数异常5");
            return responseToClient(AppError.APP_TOKEN_INVALID_ERROR);
        }

        this.token = user.getToken();

        Custommassage custommassage = new Custommassage();
        custommassage.setId(custommassageAO.getId());
        custommassage.setUserId(user.getId());
        custommassage.setCname(custommassageAO.getCname());
        custommassage.setJsonArraystr(custommassageAO.getJsonArraystr());
        custommassageService.updateByPrimaryKeySelective(custommassage);
       /* JSONArray ja=JSON.parseArray(custommassageAO.getJsonArraystr());
        for(int i=0;i<ja.size();i++) {
            System.out.println(ja.getJSONObject(i).get("id"));
            Custommassagecontent custommassagecontent = new Custommassagecontent();
            custommassagecontent.setId(Integer.parseInt(ja.getJSONObject(i).get("id")+""));
            custommassagecontent.setCid(custommassageAO.getId());
            custommassagecontent.setMassagetechnique(ja.getJSONObject(i).get("massagetechnique")+"");
            custommassagecontent.setMassagetime(ja.getJSONObject(i).get("massagetime")+"");
            custommassagecontent.setCmd(ja.getJSONObject(i).get("cmd")+"");

            custommassagecontentService.addCustommassagecontent(custommassagecontent);
        }*/

        logger.info("自定义按摩手法参数异常6");

        return responseToClient(AppError.APP_OK);

    }


}
